package com.onpositive.notes.android.main;

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RelativeLayout;

import com.onpositive.notes.android.tools.SpaceTokenizer;
import com.onpositive.notes.android.tools.TextColorProvider;
import com.onpositive.notes.android.ui.elements.UIConstants;
import com.onpositive.notes.model.INote;
import com.onpositive.notes.parser.BasicNoteParser;
import com.onpositive.notes.services.TemplateModelRegistry;

public class EditNoteActivity extends Activity {

    private static String[] STRING_REALM = null;
    
    private INote editedNote, newNote ;
    private int index = -1 ;
    private boolean delete = false ;
    
    private MultiAutoCompleteTextView txt ;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        init() ;       
        
        RelativeLayout lt = new RelativeLayout(this) ;
        initTextView();		
		RelativeLayout.LayoutParams txtParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		
		txtParams.addRule(RelativeLayout.ALIGN_PARENT_TOP) ;
		txt.setId(1001) ;
		lt.addView( txt, txtParams ) ;
		
		
		View spacer = new View(this) ;
		RelativeLayout.LayoutParams spacerParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		spacerParams.addRule( RelativeLayout.BELOW, 1001) ;
		spacerParams.addRule(RelativeLayout.ABOVE, 1002) ;
		lt.addView(spacer, spacerParams) ;
		
		LinearLayout lt2 = createButtons() ;
		lt2.setId( 1002 ) ;
		
		
		
		RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) ;
		buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL) ;
		lt.addView(lt2,buttonParams) ;		
        setContentView(lt) ;
        
    }

	private LinearLayout createButtons() {
		LinearLayout lt2 = new LinearLayout(this) ;
		
		Button deleteButton = new Button(this) ;
		deleteButton.setText("DELETE") ;		
		deleteButton.setWidth(150) ;
		lt2.addView(deleteButton,new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) ;
		deleteButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				newNote = new BasicNoteParser().parse( txt.getText().toString() ) ;
				delete = true ;
				EditNoteActivity.this.finish() ;				
			}
		}) ;
		
		Button okButton = new Button(this) ;
		okButton.setText( "OK" ) ;
		okButton.setWidth(150) ;
		lt2.addView(okButton,new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) ;
		okButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				newNote = new BasicNoteParser().parse( txt.getText().toString() ) ;				
				EditNoteActivity.this.finish() ;				
			}
		}) ;
		

		Button cancelButton = new Button(this) ;
		cancelButton.setText("CANCEL") ;		
		cancelButton.setWidth(150) ;
		lt2.addView(cancelButton,new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) ;		
		cancelButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditNoteActivity.this.finish() ;				
			}
		}) ;		 
        
		return lt2 ;
	}

	public void initTextView() {
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, STRING_REALM);
		txt = new MultiAutoCompleteTextView(this) ;
        txt.setTokenizer( new SpaceTokenizer() );
        txt.setAdapter(adapter) ;
        txt.addTextChangedListener( new TextColorProvider(txt) ) ;
        
        if(editedNote != null)
        	txt.setText( editedNote.getInitialText() ) ;
	}

    private void init() {
		if( STRING_REALM == null ){
		
			Set<String> allTemplateKeys = TemplateModelRegistry.getInstance().getAllTemplateKeys();
	        ArrayList<String> lst = new ArrayList<String>( allTemplateKeys ) ;
	        STRING_REALM = lst.toArray( new String[ lst.size() ] ) ;
		}
        
        Bundle extras = getIntent().getExtras();
        if( extras == null )
        	return ;
        
        if( extras.containsKey( UIConstants.EXTRA_KEY_NOTE ) )
        	editedNote = (INote) extras.get( UIConstants.EXTRA_KEY_NOTE ) ;
        
        if( extras.containsKey( UIConstants.EXTRA_KEY_INDEX ) )
        	index = extras.getInt( UIConstants.EXTRA_KEY_INDEX ) ;
	}

	@Override
	public void finish() {

		Intent data = new Intent();
		int requestCode = RESULT_CANCELED;
		if( newNote != null ){
			data.putExtra( UIConstants.EXTRA_KEY_NOTE, newNote );
			data.putExtra( "delete", delete) ;
			requestCode = RESULT_OK;
		}
		
		if( index >= 0 )
			data.putExtra( UIConstants.EXTRA_KEY_INDEX, index );
		
		setResult(requestCode, data);
		super.finish();
	}
	
}
